class ExploreController < ApplicationController

  def index
    # @recommends = Recommend.default_order.includes(:project).limit(9)
    @action_type = 'square'
    @projects = Project.includes(:money_requires).where('money_requires.status' => 'opened').limit(12)
    # @recommends = MoneyRequire.where("status = 'opened'").limit(12)
  end

  def all
    @heads = Head.all
    @cities = City.where("state>0 and id >1")
    @action_type = 'all'
    _where = "(money_requires.status = 'opened' or money_requires.status = 'leader_needed' or money_requires.status = 'leader_need_confirmed')"
    if !params[:city_id].blank?
      _where = _where  + " and city_id = " + params[:city_id].to_s
    end
    if !params[:head_id].blank?
      _where = _where  + " and city_id = " + params[:city_id].to_s
    end
    @projects = Project.joins(:money_requires).where(_where).page( params[:page] )
    # @projects = Project.default_order.where(_where).page( params[:page] )
    # @projects = MoneyRequire.joins(:cities_project).where(_where).page( params[:page] )
  end

  def opened
    @heads = Head.all
    @cities = City.where("state>0 and id >1")
    @action_type = 'opened'
    _where = "money_requires.status = 'opened'"
    # @projects = Project.default_order.page( params[:page] )
    @projects = Project.joins(:money_requires).where(_where).page( params[:page] )
    # @projects = MoneyRequire.where("status = 'opened'").page( params[:page] )
    render :all
  end

  def ready
    @heads = Head.all
    @cities = City.where("state>0 and id >1")
    @action_type = 'ready'
    # @projects = Project.default_order.page( params[:page] )
    _where = "(money_requires.status = 'leader_needed' or money_requires.status = 'leader_need_confirmed')"
    @projects = Project.joins(:money_requires).where(_where).page( params[:page] )
    # @projects = MoneyRequire.where("status = 'leader_needed' or status = 'leader_need_confirmed'").page( params[:page] )
    render :all
  end

  def search
    @heads = Head.all
    @cities = City.where("state>0 and id >1")
    category_ids = nil
    @action_type = 'all'

    _where = "(status = 'opened' or status = 'leader_needed' or status = 'leader_need_confirmed')"
    if !params[:city_id].blank?
      _project_id = CitiesProject.where(:city_id=>params[:city_id]).pluck(:project_id)
      _where = _where + ' and project_id in (' + _project_id.join(",") + ')'
    end
    if !params[:head_id].blank?
      _category_id = Category.where(:head_id=>params[:head_id]).pluck(:id)
      _project_id = CategoriesProject.where("category_id in (" + _category_id.join(",") + ")" ).pluck(:project_id)
      _where = _where + ' and project_id in (' + _project_id.join(",") + ')'
    end
    @projects = Project.joins(:money_requires).where(_where).page( params[:page] )
    # @projects = MoneyRequire.where(_where).page( params[:page] )

    # if params[:head_id].present?
    #   @head = Head.find(params[:head_id])
    #   category_ids = @head.categories.collect(&:id)
    # end

    # @projects = Project.search do
    #   with(:category_ids, category_ids) if category_ids
    #   with(:city_ids, [params[:city_id]]) if params[:city_id].present?
    #   fulltext "*#{params[:q]}*"
    #   paginate page: params[:page], per_page: Project::PER_PAGE
    # end.results
    render :all
  end

  def categories
    @categories = Category.all
  end

  def trend
    @action_type = 'trend'
    @how_much = case params[:how_much]
    when "most_a_week"
      :most_a_week
    when "most_a_month"
      :most_a_month
    else
      :most_a_week
    end

    count = 20

    @cond = case params[:cond]
    when "by_investments"
      @investments_most = Investment.send(@how_much, count)
      :by_investments
    when "by_stars"
      @stars_most = Star.send(@how_much, count)
      :by_stars
    when "by_times"
      @projects_most = Project.send(@how_much, count)
      :by_times
    when "by_talks"
      @talks_most = Talk.send(@how_much, count)
      :by_talks
    else
      @investments_most = Investment.most_a_week
      :by_investments
    end
  end

end
